<template>
  <div>
    <!-- 头部 -->
    <header class="site-header jumbotron">
      <div class="container">
        <div class="row">
          <div class="col-xs-12">
            <h1>请发表对Vue的评论</h1>
          </div>
        </div>
      </div>
    </header>
    <!-- 内容区 -->
    <div class="container">
      <!-- 添加评论 -->
      <AddComment :addComment="addComment" />
      <!-- 评论展示 -->
      <CommentList :comments="comments" :delComment="delComment"/>
    </div>
  </div>
</template>

<script>
import AddComment from "./views/AddComment";
import CommentList from "./views/CommentList";

export default {
  name: "App",
  components: {
    AddComment,
    CommentList,
  },
  data() {
    return {
      // 定义数据
      comments: [
        {
          id: 1,
          username: "peihua",
          content: "i like haijing",
        },
        {
          id: 2,
          username: "haijing",
          content: "i like dianlei",
        },
      ],
    };
  },
  methods: {
    // 定义更新数据的方法
    addComment(username, content) {
      this.comments.unshift({
        id: Date.now(),
        username,
        content,
      });
    },
    delComment(id) {
      // filter方法内函数返回值true代表保留，false代表过滤
      this.comments = this.comments.filter((comment) => comment.id !== id);
    },
  },
};
</script>

<style>
</style>